/*
* This file is a part of the open-eBackup project.
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright (c) [2024] Huawei Technologies Co.,Ltd.
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*/
#include "gtest/gtest.h"
#include "ObjectStorageCommonService.h"
#include "utils/PluginUtilities.h"
#include "system/System.hpp"
#include "mockcpp/mockcpp.hpp"

using namespace OBSPlugin;
using namespace std;
using namespace Module;

namespace {
    const int NUM1 = 1;
}

class ObjectStorageCommonServiceImpl : public ObjectStorageCommonService {
public:
ObjectStorageCommonServiceImpl();
~ObjectStorageCommonServiceImpl() override;
int PrerequisiteJob() override;
int GenerateSubJob() override;
int ExecuteSubJob() override;
int PostJob() override;
};

ObjectStorageCommonServiceImpl::ObjectStorageCommonServiceImpl() {}
ObjectStorageCommonServiceImpl::~ObjectStorageCommonServiceImpl() {}
int ObjectStorageCommonServiceImpl::PrerequisiteJob() {}
int ObjectStorageCommonServiceImpl::GenerateSubJob() {}
int ObjectStorageCommonServiceImpl::ExecuteSubJob() {}
int ObjectStorageCommonServiceImpl::PostJob() {}

class ObjectStorageCommonServiceTest : public testing::Test {
public:
    void SetUp();
    void TearDown();
    static void SetUpTestCase();
    static void TearDownTestCase();
    std::shared_ptr<ObjectStorageCommonServiceImpl> m_ins;
};

static int StubFunctionSucc()
{
    return 0;
}

static void ReturnVoidStub()
{
    return;
}

void ObjectStorageCommonServiceTest::SetUp()
{
    MOCKER_CPP(&sleep)
            .stubs()
            .will(invoke(StubFunctionSucc));
    m_ins = make_shared<ObjectStorageCommonServiceImpl>();
    m_ins->m_subJobInfo = std::make_shared<SubJob>();
}

void ObjectStorageCommonServiceTest::TearDown()
{
    GlobalMockObject::verify(); // 校验mock规范并清除mock规范
}

void ObjectStorageCommonServiceTest::SetUpTestCase()
{}

void ObjectStorageCommonServiceTest::TearDownTestCase()
{}

/*
 * 用例名称：checkFilePathAndGetSrcFileList
 * 前置条件：无
 * check点：检查文件路径并获得源文件列表
 **/
TEST_F(ObjectStorageCommonServiceTest, checkFilePathAndGetSrcFileList)
{
    MOCKER_CPP(&OBSPlugin::IsPathExists)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    MOCKER_CPP(&OBSPlugin::GetFileListInDirectory)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    std::string srcDir;
    std::string dstDir;
    std::vector<std::string> srcFileList;
    EXPECT_EQ(m_ins->checkFilePathAndGetSrcFileList(srcDir, dstDir, srcFileList), false);
    EXPECT_EQ(m_ins->checkFilePathAndGetSrcFileList(srcDir, dstDir, srcFileList), false);
    EXPECT_EQ(m_ins->checkFilePathAndGetSrcFileList(srcDir, dstDir, srcFileList), true);
}

/*
 * 用例名称：IsValidCtrlFile
 * 前置条件：无
 * check点：判断控制文件是否有效
 **/
TEST_F(ObjectStorageCommonServiceTest, IsValidCtrlFile)
{
    uint32_t subTaskType = 0;
    std::string ctrlFileFullPath = "txt.tmp";
    EXPECT_EQ(m_ins->IsValidCtrlFile(subTaskType, ctrlFileFullPath), false);
    subTaskType = SUBJOB_TYPE_DATACOPY_COPY_PHASE;
    ctrlFileFullPath = "/control_";
    EXPECT_EQ(m_ins->IsValidCtrlFile(subTaskType, ctrlFileFullPath), true);
    subTaskType = SUBJOB_TYPE_DATACOPY_DELETE_PHASE;
    ctrlFileFullPath = "/delete_control_";
    EXPECT_EQ(m_ins->IsValidCtrlFile(subTaskType, ctrlFileFullPath), true);
    subTaskType = SUBJOB_TYPE_DATACOPY_DIRMTIME_PHASE;
    ctrlFileFullPath = "/mtime_";
    EXPECT_EQ(m_ins->IsValidCtrlFile(subTaskType, ctrlFileFullPath), false);
    subTaskType = SUBJOB_TYPE_ANY;
    ctrlFileFullPath = "/mtime_";
    EXPECT_EQ(m_ins->IsValidCtrlFile(subTaskType, ctrlFileFullPath), false);
    subTaskType = -1;
    ctrlFileFullPath = "/delete_control_";
    EXPECT_EQ(m_ins->IsValidCtrlFile(subTaskType, ctrlFileFullPath), false);
}

/*
 * 用例名称：GetSubJobTypeByFileName
 * 前置条件：无
 * check点：获取子任务类型
 **/
TEST_F(ObjectStorageCommonServiceTest, GetSubJobTypeByFileName)
{
    std::string fileName = "/control_";
    std::string subTaskName;
    uint32_t subTaskType;
    uint32_t subTaskPrio;
    EXPECT_EQ(m_ins->GetSubJobTypeByFileName(fileName, subTaskName, subTaskType, subTaskPrio), true);
    fileName = "mtime";
    EXPECT_EQ(m_ins->GetSubJobTypeByFileName(fileName, subTaskName, subTaskType, subTaskPrio), true);
    fileName = "delete";
    EXPECT_EQ(m_ins->GetSubJobTypeByFileName(fileName, subTaskName, subTaskType, subTaskPrio), true);
    fileName = "xxx";
    EXPECT_EQ(m_ins->GetSubJobTypeByFileName(fileName, subTaskName, subTaskType, subTaskPrio), true);

    SubJob subJob;
    std::string jobId;
    std::string ctrlFile;
    std::string ext;
    MOCKER_CPP(&Module::JsonHelper::StructToJsonString<ObjectStorageBackupSubJob>)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    EXPECT_EQ(m_ins->InitSubTask(subJob, jobId, ctrlFile, subTaskType, ext), false);
    EXPECT_EQ(m_ins->InitSubTask(subJob, jobId, ctrlFile, subTaskType, ext), true);
}
/*
 * 用例名称：CreateSubTask
 * 前置条件：无
 * check点：创建子任务
 **/

static void AddNewJobSub1()
{
    return;
}

TEST_F(ObjectStorageCommonServiceTest, CreateSubTaskList)
{
    std::vector<SubJob> subJobList {};
    std::vector<std::string> ctrlFileList {};
    EXPECT_EQ(m_ins->CreateSubTask(subJobList, ctrlFileList), Module::SUCCESS);
}

TEST_F(ObjectStorageCommonServiceTest, CreateSubTaskList1)
{
    std::vector<SubJob> subJobList {};
    std::vector<std::string> ctrlFileList {};
    SubJob tmpjob;
    subJobList.emplace_back(tmpjob);
    ctrlFileList.emplace_back("test");
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::KeepJobAlive)
            .stubs()
            .will(invoke(ReturnVoidStub));
    MOCKER_CPP(&BasicJob::IsAbortJob)
            .stubs()
            .will(returnValue(true));
    MOCKER_CPP(&OBSPlugin::RemoveFile)
            .stubs()
            .will(returnValue(true));
    MOCKER_CPP(&NodeLevelTaskInfo::IncrSubTasksSubmitCount)
            .stubs()
            .will(invoke(ReturnVoidStub));
    EXPECT_EQ(m_ins->CreateSubTask(subJobList, ctrlFileList), Module::SUCCESS);
}

TEST_F(ObjectStorageCommonServiceTest, CreateSubTask1)
{
    SubJob subJob;
    MOCKER_CPP(&BasicJob::IsAbortJob)
            .stubs()
            .will(returnValue(true))
            .then(returnValue(false));
    MOCKER_CPP(&JobService::AddNewJob)
            .stubs()
            .will(invoke(AddNewJobSub1));
    EXPECT_EQ(m_ins->CreateSubTask(subJob), Module::SUCCESS);
    EXPECT_EQ(m_ins->CreateSubTask(subJob), Module::SUCCESS);
}

/*
 * 用例名称：ReportJobDetails
 * 前置条件：无
 * check点：上报工作内容
 **/
TEST_F(ObjectStorageCommonServiceTest, ReportJobDetails)
{
    MOCKER_CPP(&JobService::ReportJobDetails)
            .stubs()
            .will(ignoreReturnValue());
    LogDetail logDetail;
    AppProtect::JobLogLevel::type jobLogLevel {};
    logDetail.level = jobLogLevel;
    logDetail.description = "xxx";
    SubJobDetails jobInfo;
    jobInfo.logDetail.push_back(logDetail);
    EXPECT_EQ(m_ins->ReportJobDetails(jobInfo), true);
}

/*
 * 用例名称：CreateResource
 * 前置条件：无
 * check点：创建资源
 **/
TEST_F(ObjectStorageCommonServiceTest, CreateResource)
{
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::KeepJobAlive)
            .stubs()
            .will(invoke(ReturnVoidStub));
    MOCKER_CPP(&ShareResource::CreateResource)
            .stubs()
            .will(ignoreReturnValue());
    std::string key;
    std::string val;
    std::string mainJobId;
    std::string subJobId;
    EXPECT_EQ(m_ins->CreateResource(key, val, mainJobId, subJobId), true);
}

/*
 * 用例名称：QueryResource
 * 前置条件：无
 * check点：查询资源
 **/
TEST_F(ObjectStorageCommonServiceTest, QueryResource)
{
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::KeepJobAlive)
            .stubs()
            .will(invoke(ReturnVoidStub));
    MOCKER_CPP(ShareResource::QueryResource)
            .stubs()
            .will(throws(AppProtectFrameworkException()))
            .then(ignoreReturnValue());
    std::string key;
    AppProtect::ResourceStatus ret;
    EXPECT_EQ(m_ins->QueryResource(key, ret), true);
    EXPECT_EQ(m_ins->QueryResource(key, ret), true);
}

/*
 * 用例名称：UpdateResource
 * 前置条件：无
 * check点：更新资源
 **/
TEST_F(ObjectStorageCommonServiceTest, UpdateResource)
{
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::KeepJobAlive)
            .stubs()
            .will(invoke(ReturnVoidStub));
    MOCKER_CPP(&ShareResource::UpdateResource)
            .stubs()
            .will(invoke(ReturnVoidStub));
    std::string key;
    Resource res;
    std::string mainJobId;
    std::string subJobId;
    EXPECT_EQ(m_ins->UpdateResource(key, res, mainJobId, subJobId), true);
}

/*
 * 用例名称：DeleteResource
 * 前置条件：无
 * check点：删除资源
 **/
TEST_F(ObjectStorageCommonServiceTest, DeleteResource)
{
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::KeepJobAlive)
            .stubs()
            .will(invoke(ReturnVoidStub));
    MOCKER_CPP(&ShareResource::DeleteResource)
            .stubs()
            .will(invoke(ReturnVoidStub));
    std::string key;
    std::string mainJobId;
    std::string subJobId;
    EXPECT_EQ(m_ins->DeleteResource(key, mainJobId, subJobId), true);
}

/*
 * 用例名称：LockResource
 * 前置条件：无
 * check点：锁定资源
 **/
TEST_F(ObjectStorageCommonServiceTest, LockResource)
{
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::KeepJobAlive)
            .stubs()
            .will(invoke(ReturnVoidStub));
    MOCKER_CPP(&ShareResource::LockResource)
            .stubs()
            .will(invoke(ReturnVoidStub));
    std::string key;
    uint32_t maxRetryCount = 0;
    std::string mainJobId;
    EXPECT_EQ(m_ins->LockResource(key, maxRetryCount, mainJobId), true);
}

/*
 * 用例名称：UnlockResource
 * 前置条件：无
 * check点：解锁资源
 **/
TEST_F(ObjectStorageCommonServiceTest, UnlockResource)
{
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::KeepJobAlive)
            .stubs()
            .will(invoke(ReturnVoidStub));
    MOCKER_CPP(&ShareResource::UnLockResource)
            .stubs()
            .will(invoke(ReturnVoidStub));
    std::string key;
    uint32_t maxRetryCount = 0;
    std::string mainJobId;
    EXPECT_EQ(m_ins->UnlockResource(key, maxRetryCount, mainJobId), true);
}

/*
 * 用例名称：CreateScanStatsResource
 * 前置条件：无
 * check点：创建扫描打点资源
 **/
TEST_F(ObjectStorageCommonServiceTest, CreateScanStatsResource)
{
    MOCKER_CPP(&Module::JsonHelper::StructToJsonString<ObjectStorageNativeScanStatistics>)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::CreateResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    std::string key;
    ObjectStorageNativeScanStatistics scanStats;
    EXPECT_EQ(m_ins->CreateScanStatsResource(key, scanStats), false);
    EXPECT_EQ(m_ins->CreateScanStatsResource(key, scanStats), false);
    EXPECT_EQ(m_ins->CreateScanStatsResource(key, scanStats), true);
}

/*
 * 用例名称：GetScanStatsResource
 * 前置条件：无
 * check点：查询扫描资源
 **/
TEST_F(ObjectStorageCommonServiceTest, GetScanStatsResource)
{
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::QueryResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    MOCKER_CPP(&Module::JsonHelper::JsonStringToStruct<ObjectStorageNativeScanStatistics>)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    std::string key;
    ObjectStorageNativeScanStatistics scanStats;
    EXPECT_EQ(m_ins->GetScanStatsResource(key, scanStats), false);
    EXPECT_EQ(m_ins->GetScanStatsResource(key, scanStats), false);
    EXPECT_EQ(m_ins->GetScanStatsResource(key, scanStats), true);
}

/*
 * 用例名称：UpdateScanStatsResource
 * 前置条件：无
 * check点：更新扫描资源
 **/
TEST_F(ObjectStorageCommonServiceTest, UpdateScanStatsResource)
{
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::UpdateResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    MOCKER_CPP(&Module::JsonHelper::StructToJsonString<ObjectStorageNativeScanStatistics>)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    std::string key;
    ObjectStorageNativeScanStatistics scanStats;
    EXPECT_EQ(m_ins->UpdateScanStatsResource(key, scanStats), false);
    EXPECT_EQ(m_ins->UpdateScanStatsResource(key, scanStats), false);
    EXPECT_EQ(m_ins->UpdateScanStatsResource(key, scanStats), true);
}

/*
 * 用例名称：UpdateScanStatsResource
 * 前置条件：无
 * check点：更新扫描资源
 **/
TEST_F(ObjectStorageCommonServiceTest, DeleteScanStatsResource)
{
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::DeleteResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    std::string key;

    EXPECT_EQ(m_ins->DeleteScanStatsResource(key), false);
    EXPECT_EQ(m_ins->DeleteScanStatsResource(key), true);
}

/*
 * 用例名称：LockBackupStatsResource
 * 前置条件：无
 * check点：锁住扫描资源
 **/
TEST_F(ObjectStorageCommonServiceTest, LockBackupStatsResource)
{
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::LockResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    std::string mainJobId;
    EXPECT_EQ(m_ins->LockBackupStatsResource(mainJobId), false);
    EXPECT_EQ(m_ins->LockBackupStatsResource(mainJobId), true);
}

/*
 * 用例名称：UnlockBackupStatsResource
 * 前置条件：无
 * check点：解锁扫描资源
 **/
TEST_F(ObjectStorageCommonServiceTest, UnlockBackupStatsResource)
{
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::UnlockResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    std::string mainJobId;
    EXPECT_EQ(m_ins->UnlockBackupStatsResource(mainJobId), false);
    EXPECT_EQ(m_ins->UnlockBackupStatsResource(mainJobId), true);
}

/*
 * 用例名称：CreateBackupStatsResource
 * 前置条件：无
 * check点：创建备份打点资源
 **/
TEST_F(ObjectStorageCommonServiceTest, CreateBackupStatsResource)
{
    MOCKER_CPP(&Module::JsonHelper::StructToJsonString<ObjectStorageNativeBackupStats>)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::CreateResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    std::string key;
    ObjectStorageNativeBackupStats scanStats;
    EXPECT_EQ(m_ins->CreateBackupStatsResource(key, scanStats), false);
    EXPECT_EQ(m_ins->CreateBackupStatsResource(key, scanStats), false);
    EXPECT_EQ(m_ins->CreateBackupStatsResource(key, scanStats), true);
}

/*
 * 用例名称：GetBackupStatsResource
 * 前置条件：无
 * check点：查询备份资源
 **/
TEST_F(ObjectStorageCommonServiceTest, GetBackupStatsResource)
{
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::QueryResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    MOCKER_CPP(&Module::JsonHelper::JsonStringToStruct<ObjectStorageNativeBackupStats>)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    std::string key;
    ObjectStorageNativeBackupStats scanStats;
    EXPECT_EQ(m_ins->GetBackupStatsResource(key, scanStats), false);
    EXPECT_EQ(m_ins->GetBackupStatsResource(key, scanStats), false);
    EXPECT_EQ(m_ins->GetBackupStatsResource(key, scanStats), true);
}

/*
 * 用例名称：UpdateBackupStatsResource
 * 前置条件：无
 * check点：更新备份资源
 **/
TEST_F(ObjectStorageCommonServiceTest, UpdateBackupStatsResource)
{
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::UpdateResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    MOCKER_CPP(&Module::JsonHelper::StructToJsonString<ObjectStorageNativeBackupStats>)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    std::string key;
    ObjectStorageNativeBackupStats backupStats;
    EXPECT_EQ(m_ins->UpdateBackupStatsResource(key, backupStats), false);
    EXPECT_EQ(m_ins->UpdateBackupStatsResource(key, backupStats), false);
    EXPECT_EQ(m_ins->UpdateBackupStatsResource(key, backupStats), true);
}

/*
 * 用例名称：DeleteBackupStatsResource
 * 前置条件：无
 * check点：删除备份资源
 **/
TEST_F(ObjectStorageCommonServiceTest, DeleteBackupStatsResource)
{
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::DeleteResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    std::string key;

    EXPECT_EQ(m_ins->DeleteBackupStatsResource(key), false);
    EXPECT_EQ(m_ins->DeleteBackupStatsResource(key), true);
}

/*
 * 用例名称：LockGeneralResource
 * 前置条件：无
 * check点：锁住资源
 **/
TEST_F(ObjectStorageCommonServiceTest, LockGeneralResource)
{
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::LockResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    std::string mainJobId;
    EXPECT_EQ(m_ins->LockGeneralResource(mainJobId), false);
    EXPECT_EQ(m_ins->LockGeneralResource(mainJobId), true);
}

/*
 * 用例名称：UnlockGeneralResource
 * 前置条件：无
 * check点：解锁资源
 **/
TEST_F(ObjectStorageCommonServiceTest, UnlockGeneralResource)
{
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::UnlockResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    std::string mainJobId;
    EXPECT_EQ(m_ins->UnlockGeneralResource(mainJobId), false);
    EXPECT_EQ(m_ins->UnlockGeneralResource(mainJobId), true);
}

/*
 * 用例名称：CreateGeneralResource
 * 前置条件：无
 * check点：创建资源
 **/
TEST_F(ObjectStorageCommonServiceTest, CreateGeneralResource)
{
    MOCKER_CPP(&Module::JsonHelper::StructToJsonString<ObjectStorageNativeGeneral>)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::CreateResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::GetGeneralResource)
            .stubs()
            .will(returnValue(true))
            .then(returnValue(false));
    std::string key;
    ObjectStorageNativeGeneral generalInfo;
    EXPECT_EQ(m_ins->CreateGeneralResource(key, generalInfo), true);
    EXPECT_EQ(m_ins->CreateGeneralResource(key, generalInfo), false);
    EXPECT_EQ(m_ins->CreateGeneralResource(key, generalInfo), false);
    EXPECT_EQ(m_ins->CreateGeneralResource(key, generalInfo), true);
}

/*
 * 用例名称：GetGlobalScanRedoResource
 * 前置条件：无
 * check点：获取资源
 **/
TEST_F(ObjectStorageCommonServiceTest, GetGeneralResource)
{
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::QueryResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    MOCKER_CPP(&Module::JsonHelper::JsonStringToStruct<ObjectStorageNativeGeneral>)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    std::string key;
    ObjectStorageNativeGeneral generalInfo;
    EXPECT_EQ(m_ins->GetGeneralResource(key, generalInfo), false);
    EXPECT_EQ(m_ins->GetGeneralResource(key, generalInfo), false);
    EXPECT_EQ(m_ins->GetGeneralResource(key, generalInfo), true);
}

/*
 * 用例名称：UpdateGlobalScanRedoList
 * 前置条件：无
 * check点：更新资源
 **/
TEST_F(ObjectStorageCommonServiceTest, UpdateGeneralResource)
{
    MOCKER_CPP(&Module::JsonHelper::StructToJsonString<ObjectStorageNativeGeneral>)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::UpdateResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    std::string key;
    ObjectStorageNativeGeneral generalInfo;
    EXPECT_EQ(m_ins->UpdateGeneralResource(key, generalInfo), false);
    EXPECT_EQ(m_ins->UpdateGeneralResource(key, generalInfo), false);
    EXPECT_EQ(m_ins->UpdateGeneralResource(key, generalInfo), true);
}

/*
 * 用例名称：DeleteGeneralResource
 * 前置条件：无
 * check点：删除资源
 **/
TEST_F(ObjectStorageCommonServiceTest, DeleteGeneralResource)
{
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::DeleteResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    std::string key;
    EXPECT_EQ(m_ins->DeleteGeneralResource(key), false);
    EXPECT_EQ(m_ins->DeleteGeneralResource(key), true);
}

/*
 * 用例名称：GetCurrentTimeFromRemoteServer
 * 前置条件：无
 * check点：获取时间
 **/
TEST_F(ObjectStorageCommonServiceTest, GetCurrentTimeFromRemoteServer)
{
    std::string path = "/home";
    MOCKER_CPP(boost::filesystem::remove, bool(const boost::filesystem::path&))
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    EXPECT_EQ(m_ins->GetCurrentTimeFromRemoteServer(path), 0);
    EXPECT_NO_THROW(m_ins->GetCurrentTimeFromRemoteServer(path));
}

TEST_F(ObjectStorageCommonServiceTest, GetCurrentTimeFromRemoteServer1)
{
    std::string path = "/home";
    string str = "err";
    boost::filesystem::path path1 = "/a/b.txt";
    boost::filesystem::path path2 = "/a/b.txt";
    boost::system::error_code ec;
    MOCKER_CPP(boost::filesystem::remove, bool(const boost::filesystem::path&))
            .stubs()
            .will(throws(boost::filesystem::filesystem_error(str, path1, path2, ec)));
    EXPECT_EQ(m_ins->GetCurrentTimeFromRemoteServer(path), 0);
}

/*
 * 用例名称：CreateUniqueTempFileName
 * 前置条件：无
 * check点：创建文件名
 **/
TEST_F(ObjectStorageCommonServiceTest, CreateUniqueTempFileName)
{
    std::string path = "/home";
    EXPECT_NO_THROW(m_ins->CreateUniqueTempFileName(path));
}

TEST_F(ObjectStorageCommonServiceTest, CreateUniqueTempFileName1)
{
    std::string path = "/home";
    string str = "err";
    boost::filesystem::path path1 = "/a/b.txt";
    boost::filesystem::path path2 = "/a/b.txt";
    boost::system::error_code ec;
    MOCKER_CPP(boost::filesystem::exists, bool(const boost::filesystem::path&))
            .stubs()
            .will(throws(boost::filesystem::filesystem_error(str, path1, path2, ec)));
    EXPECT_NO_THROW(m_ins->CreateUniqueTempFileName(path));
}

/*
 * 用例名称：WriteBackupStatsToFile
 * 前置条件：无
 * check点：将打点写入文件
 **/
TEST_F(ObjectStorageCommonServiceTest, WriteBackupStatsToFile)
{
    MOCKER_CPP(&Module::JsonHelper::StructToJsonString<ObjectStorageNativeBackupStats>)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    MOCKER_CPP(&OBSPlugin::WriteFile)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    std::string filePath;
    ObjectStorageNativeBackupStats backupStatistics;
    EXPECT_EQ(m_ins->WriteBackupStatsToFile(filePath, backupStatistics), false);
    EXPECT_EQ(m_ins->WriteBackupStatsToFile(filePath, backupStatistics), false);
    EXPECT_EQ(m_ins->WriteBackupStatsToFile(filePath, backupStatistics), true);
}

/*
 * 用例名称：ReadBackupStatsFromFile
 * 前置条件：无
 * check点：从文件重读取打点
 **/
TEST_F(ObjectStorageCommonServiceTest, ReadBackupStatsFromFile)
{
    MOCKER_CPP(&OBSPlugin::ReadFile)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    std::string filePath;
    ObjectStorageNativeBackupStats backupStatistics;

    EXPECT_EQ(m_ins->ReadBackupStatsFromFile(filePath, backupStatistics), false);
    EXPECT_EQ(m_ins->ReadBackupStatsFromFile(filePath, backupStatistics), true);
}

/*
 * 用例名称：CreateSharedResources
 * 前置条件：无
 * check点：创建资源
 **/
TEST_F(ObjectStorageCommonServiceTest, CreateSharedResources)
{
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::DeleteGeneralResource)
            .stubs()
            .will(returnValue(true));
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::DeleteScanStatsResource)
            .stubs()
            .will(returnValue(true));
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::DeleteBackupStatsResource)
            .stubs()
            .will(returnValue(true));
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::CreateGeneralResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::CreateScanStatsResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));

    MOCKER_CPP(&ObjectStorageCommonServiceImpl::CreateBackupStatsResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));

    std::string jobId;
    ObjectStorageNativeGeneral generalInfo;
    ObjectStorageNativeScanStatistics scanStats;
    ObjectStorageNativeBackupStats backupStats;
    EXPECT_EQ(m_ins->CreateSharedResources(jobId, generalInfo, scanStats, backupStats), false);
    EXPECT_EQ(m_ins->CreateSharedResources(jobId, generalInfo, scanStats, backupStats), false);
    EXPECT_EQ(m_ins->CreateSharedResources(jobId, generalInfo, scanStats, backupStats), false);
    EXPECT_EQ(m_ins->CreateSharedResources(jobId, generalInfo, scanStats, backupStats), true);
}

/*
 * 用例名称：GetSharedResources
 * 前置条件：无
 * check点：获取共享资源
 **/
TEST_F(ObjectStorageCommonServiceTest, GetSharedResources)
{
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::GetGeneralResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::GetScanStatsResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::GetBackupStatsResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    std::string jobId;
    ObjectStorageNativeGeneral generalInfo;
    ObjectStorageNativeScanStatistics scanStats;
    ObjectStorageNativeBackupStats backupStats;
    EXPECT_EQ(m_ins->GetSharedResources(jobId, generalInfo, scanStats, backupStats), false);
    EXPECT_EQ(m_ins->GetSharedResources(jobId, generalInfo, scanStats, backupStats), false);
    EXPECT_EQ(m_ins->GetSharedResources(jobId, generalInfo, scanStats, backupStats), false);
    EXPECT_EQ(m_ins->GetSharedResources(jobId, generalInfo, scanStats, backupStats), true);
}

/*
 * 用例名称：DeleteSharedResources
 * 前置条件：无
 * check点：删除共享资源
 **/
TEST_F(ObjectStorageCommonServiceTest, DeleteSharedResources)
{
    MOCKER_CPP(&ScanMgr::ReleaseRunningTask)
            .stubs()
            .will(returnValue(true));
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::DeleteGeneralResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::DeleteScanStatsResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::DeleteBackupStatsResource)
            .stubs()
            .will(returnValue(false))
            .then(returnValue(true));
    std::string jobId;
    EXPECT_EQ(m_ins->DeleteSharedResources(jobId), true);
    EXPECT_EQ(m_ins->DeleteSharedResources(jobId), true);
    EXPECT_EQ(m_ins->DeleteSharedResources(jobId), true);
    EXPECT_EQ(m_ins->DeleteSharedResources(jobId), true);
    EXPECT_EQ(m_ins->DeleteSharedResources(jobId), true);
}

/*
 * 用例名称：UpdateScannerStatistics
 * 前置条件：无
 * check点：更新共享资源
 **/
TEST_F(ObjectStorageCommonServiceTest, UpdateScannerStatistics)
{
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::PrintScannerStatistics)
            .stubs()
            .will(invoke(ReturnVoidStub));
    ObjectStorageNativeScanStatistics scanStatistics;
    ObjectStorageNativeScanStatistics scanStats;
    bool isScannerRestarted = false;
    std::string jobId;
    isScannerRestarted = true;
    EXPECT_EQ(m_ins->UpdateScannerStatistics(scanStatistics, scanStats), true);
}

/*
 * 用例名称：AddScanStatistics
 * 前置条件：无
 * check点：添加扫描打点
 **/
TEST_F(ObjectStorageCommonServiceTest, AddScanStatistics)
{
    ScanStatistics stats_A;
    ScanStatistics stats_B;
    EXPECT_NO_THROW(m_ins->AddScanStatistics(stats_A, stats_B));
}

/*
 * 用例名称：PrintScannerStatistics
 * 前置条件：无
 * check点：打印扫描打点
 **/
TEST_F(ObjectStorageCommonServiceTest, PrintScannerStatistics)
{
    ObjectStorageNativeScanStatistics nativeScanStats;
    nativeScanStats.m_scanStatus = (int)SCANNER_STATUS::SCAN_COMPLETED;
    EXPECT_NO_THROW(m_ins->PrintScannerStatistics(nativeScanStats));
}

/*
 * 用例名称：PrintScannerStatistics
 * 前置条件：无
 * check点：打印扫描打点
 **/
TEST_F(ObjectStorageCommonServiceTest, PrintBackupStatistics)
{
    BackupStats backupStatistics;
    std::string jobId;
    BackupPhaseStatus m_backupStatus;
    std::string statPrintSubJobId;
    EXPECT_NO_THROW(m_ins->PrintBackupStatistics(backupStatistics, jobId, m_backupStatus, statPrintSubJobId));
}

/*
 * 用例名称：UpdateBackupSubTaskStatistics
 * 前置条件：无
 * check点：更新备份打点
 **/
TEST_F(ObjectStorageCommonServiceTest, UpdateBackupSubTaskStatistics)
{
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::WriteBackupStatsToFile)
            .stubs()
            .will(returnValue(true));
    BackupStats backupStatistics;
    uint32_t subTaskType = 0;
    std::string metaFsPath;
    std::shared_ptr<SubJob> subJobInfo = make_shared<SubJob>();
    EXPECT_EQ(
        m_ins->UpdateBackupSubTaskStatistics(backupStatistics, metaFsPath, 0, 0),
        true);
    subTaskType = SUBJOB_TYPE_DATACOPY_DIRMTIME_PHASE;
    EXPECT_EQ(
        m_ins->UpdateBackupSubTaskStatistics(backupStatistics, metaFsPath, 0, 0),
        true);
}

/*
 * 用例名称:PrintSubJobInfo
 * 前置条件：无
 * check点：打印信息输出到日志
 */
TEST_F(ObjectStorageCommonServiceTest, PrintSubJobInfo)
{
    shared_ptr<SubJob> subJob = make_shared<AppProtect::SubJob>();
    EXPECT_NO_THROW(m_ins->PrintSubJobInfo(subJob));
}

/*
 * 用例名称：IsUpdateBackupStartTimeRequired
 * 前置条件：无
 * check点：判断备份开始时间
 **/
TEST_F(ObjectStorageCommonServiceTest, IsUpdateBackupStartTimeRequired)
{
    ObjectStorageBackupSubJob backupSubJob;
    ObjectStorageNativeGeneral generalInfo;
    backupSubJob.m_SubTaskType = OBSPlugin::SUBJOB_TYPE_DATACOPY_COPY_PHASE;
    generalInfo.m_backupCopyPhaseStartTime == 0;
    EXPECT_EQ(m_ins->IsUpdateBackupStartTimeRequired(backupSubJob, generalInfo), true);
    generalInfo.m_backupCopyPhaseStartTime == NUM1;
    generalInfo.m_restoreCopyIndex = 0;
    backupSubJob.orderNumberForAggregate = NUM1;
    EXPECT_EQ(m_ins->IsUpdateBackupStartTimeRequired(backupSubJob, generalInfo), true);
    backupSubJob.m_SubTaskType = OBSPlugin::SUBJOB_TYPE_DATACOPY_DELETE_PHASE;
    generalInfo.m_backupDelPhaseStartTime == 0;
    EXPECT_EQ(m_ins->IsUpdateBackupStartTimeRequired(backupSubJob, generalInfo), true);
    generalInfo.m_backupCopyPhaseStartTime == NUM1;
    generalInfo.m_restoreCopyIndex = 0;
    backupSubJob.orderNumberForAggregate = NUM1;
    EXPECT_EQ(m_ins->IsUpdateBackupStartTimeRequired(backupSubJob, generalInfo), true);
    backupSubJob.m_SubTaskType = OBSPlugin::SUBJOB_TYPE_ANY;
    EXPECT_EQ(m_ins->IsUpdateBackupStartTimeRequired(backupSubJob, generalInfo), false);
}

/*
 * 用例名称:PrintFinalBackupStats
 * 前置条件：无
 * check点：打印信息
 */
TEST_F(ObjectStorageCommonServiceTest, PrintFinalBackupStats)
{
    ObjectStorageNativeBackupStats backupStatistics;
    EXPECT_EQ(m_ins->PrintFinalBackupStats(backupStatistics), true);
}

/*
 * 用例名称:PrintFinalScannerStats
 * 前置条件：无
 * check点：打印信息
 */
TEST_F(ObjectStorageCommonServiceTest, PrintFinalScannerStats)
{
    ObjectStorageNativeScanStatistics scanStats;
    scanStats.m_scanStatus = (int)SCANNER_STATUS::SCAN_COMPLETED;
    EXPECT_EQ(m_ins->PrintFinalScannerStats(scanStats), true);
    scanStats.m_scanStatus = (int)SCANNER_STATUS::SUCCESS;
    EXPECT_EQ(m_ins->PrintFinalScannerStats(scanStats), true);
}

/*
 * 用例名称:PrintBackupScanFilter
 * 前置条件：无
 * check点：打印信息
 */
TEST_F(ObjectStorageCommonServiceTest, PrintBackupScanFilter)
{
    ScanDirectoryFilter dirFilter;
    ScanFileFilter fileFilter;
    fileFilter.fileList.emplace_back("test");
    dirFilter.dirList.emplace_back("test");
    EXPECT_NO_THROW(m_ins->PrintBackupScanFilter(dirFilter, fileFilter));
}

/*
 * 用例名称:SetJobCtrlPhase
 * 前置条件：无
 * check点：设置ctrl
 */
TEST_F(ObjectStorageCommonServiceTest, SetJobCtrlPhase)
{
    std::string jobCtrlPhase = "test";
    EXPECT_NO_THROW(m_ins->SetJobCtrlPhase(jobCtrlPhase));
}

/*
 * 用例名称:SetMainJobId
 * 前置条件：无
 * check点：设置jobid
 */
TEST_F(ObjectStorageCommonServiceTest, SetMainJobId)
{
    std::string jobId = "test";
    EXPECT_NO_THROW(m_ins->SetMainJobId(jobId));
}

/*
 * 用例名称:SetMainJobId
 * 前置条件：无
 * check点：设置jobid
 */
TEST_F(ObjectStorageCommonServiceTest, SetSubJobId)
{
    EXPECT_NO_THROW(m_ins->SetSubJobId());
}

/*
 * 用例名称:FillScanConfigForNative
 * 前置条件：无
 * check点：填充扫描配置
 */
TEST_F(ObjectStorageCommonServiceTest, FillScanConfigForNative)
{
    ScanConfig scanConfig;
    EXPECT_NO_THROW(m_ins->FillScanConfigForNative(scanConfig));
}

/*
 * 用例名称：KeepJobAlive
 * 前置条件：无
 * check点：心跳
 **/
TEST_F(ObjectStorageCommonServiceTest, KeepJobAlive)
{
    MOCKER_CPP(&JobService::ReportJobDetails)
            .stubs()
            .will(returnValue(Module::SUCCESS))
            .then(returnValue(Module::FAILED));
    string mainJobId = "main";
    string subJobId = "sub";
    EXPECT_NO_THROW(m_ins->KeepJobAlive(mainJobId, subJobId));
    EXPECT_NO_THROW(m_ins->KeepJobAlive(mainJobId, subJobId));
}

/*
 * 用例名称: SendJobReportForAliveness
 * 前置条件：无
 * check点：SendJob
 */
TEST_F(ObjectStorageCommonServiceTest, SendJobReportForAliveness)
{
    MOCKER_CPP(&JobService::ReportJobDetails)
            .stubs()
            .will(returnValue(Module::SUCCESS));
    EXPECT_TRUE(m_ins->SendJobReportForAliveness());
}

/*
 * 用例名称: SendJobReportForAliveness
 * 前置条件：无
 * check点：SendJob
 */
TEST_F(ObjectStorageCommonServiceTest, SleepForCreateSubTaskError)
{
    MOCKER_CPP(&BasicJob::IsAbortJob)
            .stubs()
            .will(returnValue(true))
            .then(returnValue(false));
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::SendJobReportForAliveness)
            .stubs()
            .will(returnValue(true));
    EXPECT_NO_THROW(m_ins->SleepForCreateSubTaskError());
    EXPECT_NO_THROW(m_ins->SleepForCreateSubTaskError());
}

/*
 * 用例名称: CheckAndRetryCreateSubTask
 * 前置条件：无
 * check点：SendJob
 */
TEST_F(ObjectStorageCommonServiceTest, CheckAndRetryCreateSubTask)
{
    MOCKER_CPP(&BasicJob::IsAbortJob)
            .stubs()
            .will(returnValue(true))
            .then(returnValue(false));
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::CreateSubTask,
                int(ObjectStorageCommonServiceImpl::*)(std::vector<SubJob>&, std::vector<std::string>&))
            .stubs()
            .will(returnValue(Module::RETRY))
            .then((returnValue(Module::RETRY)))
            .then((returnValue(Module::FAILED)))
            .then(returnValue(Module::SUCCESS));
    MOCKER_CPP(&ObjectStorageCommonServiceImpl::SleepForCreateSubTaskError)
            .stubs()
            .will(invoke(ReturnVoidStub));
    std::vector<SubJob> subJobList;
    std::vector<std::string> ctrlFileList;
    int64_t createJobErrTime;
    bool isFinal = false;
    EXPECT_EQ(m_ins->CheckAndRetryCreateSubTask(subJobList, ctrlFileList, createJobErrTime, isFinal), true);
    SubJob tmpSubJob;
    subJobList.emplace_back(tmpSubJob);
    ctrlFileList.emplace_back("test");
    EXPECT_EQ(m_ins->CheckAndRetryCreateSubTask(subJobList, ctrlFileList, createJobErrTime, isFinal), true);
    EXPECT_EQ(m_ins->CheckAndRetryCreateSubTask(subJobList, ctrlFileList, createJobErrTime, isFinal), true);
    tmpSubJob;
    subJobList.emplace_back(tmpSubJob);
    ctrlFileList.emplace_back("test");
    isFinal = true;
    EXPECT_EQ(m_ins->CheckAndRetryCreateSubTask(subJobList, ctrlFileList, createJobErrTime, isFinal), false);
    EXPECT_EQ(m_ins->CheckAndRetryCreateSubTask(subJobList, ctrlFileList, createJobErrTime, isFinal), true);
}

/*
 * 用例名称: MergeBackupFailureRecords
 * 前置条件：无
 * check点：合并错误统计
 */
TEST_F(ObjectStorageCommonServiceTest, MergeBackupFailureRecords)
{
    EXPECT_NO_THROW(m_ins->MergeBackupFailureRecords());
}

TEST_F(ObjectStorageCommonServiceTest, RemoveScanStatFile)
{
    MOCKER_CPP(&OBSPlugin::RemoveFile)
       .stubs()
       .will(returnValue(true));
    PrefixInfo prefixInfo;
    std::string subJobId;
    std::string cacheFsPath;
    EXPECT_NO_THROW(m_ins->RemoveScanStatFile(prefixInfo, subJobId, cacheFsPath));
}

TEST_F(ObjectStorageCommonServiceTest, UnlockScanStatsResource)
{
    MOCKER_CPP(&ShareResource::UnLockResource)
        .stubs()
        .will(invoke(ReturnVoidStub));
    EXPECT_EQ(m_ins->UnlockScanStatsResource("123"), true);
}

TEST_F(ObjectStorageCommonServiceTest, UpdateScanStatsResourceWithLock)
{
    MOCKER_CPP(&ShareResource::LockResource)
        .stubs()
        .will(invoke(ReturnVoidStub));
    MOCKER_CPP(&ShareResource::UnLockResource)
        .stubs()
        .will(invoke(ReturnVoidStub));
    MOCKER_CPP(&ShareResource::UpdateResource)
        .stubs()
        .will(invoke(ReturnVoidStub));
    MOCKER_CPP(&ObjectStorageCommonService::GetScanStatsResource)
        .stubs()
        .will(returnValue(true));
    ObjectStorageNativeScanStatistics scanStatistics;
    bool bReportToPM = false;
    EXPECT_EQ(m_ins->UpdateScanStatsResourceWithLock(scanStatistics, bReportToPM), true);
}


TEST_F(ObjectStorageCommonServiceTest, CalcSubScanStats)
{
    MOCKER_CPP(&OBSPlugin::GetFileListInDirectory)
        .stubs()
        .will(returnValue(true));
    MOCKER_CPP(&JobService::ReportJobDetails)
        .stubs()
        .will(ignoreReturnValue());
    MOCKER_CPP(&OBSPlugin::RemoveFile)
        .stubs()
        .will(returnValue(true));
    MOCKER(JsonFileTool::ReadFromFile<OBSPlugin::ObjectStorageNativeScanStatistics>)
        .stubs()
        .will(returnValue(true));
    ObjectStorageNativeScanStatistics scanStatistics;
    EXPECT_EQ(m_ins->CalcSubScanStats("path", true, scanStatistics), true);
}
